package com.demo.java.OD351_400.OD378;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【寻找核酸检测点(B卷)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146237588
 */
public class OdMain {
    private static final int MAX_WAIT_TIME = 480; // 8:00 AM
    private static final int FIRST_STAGE_END_TIME = 600; // 10:00 AM
    private static final int SECOND_STAGE_END_TIME = 720; // 12:00 PM
    private static final int THIRD_STAGE_END_TIME = 840; // 2:00 PM

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            // 读取开始时间和结束时间
            List<Integer> startTimeInfo = parseInput(scanner.nextLine());
            int start = calculateTimeInMinutes(startTimeInfo);

            List<Integer> endTimeInfo = parseInput(scanner.nextLine());
            int end = calculateTimeInMinutes(endTimeInfo);

            // 读取任务数目
            int numberOfPoints = Integer.parseInt(scanner.nextLine());

            List<List<Integer>> points = new ArrayList<>();
            for (int i = 0; i < numberOfPoints; i++) {
                List<Integer> pointInfo = parseInput(scanner.nextLine());
                int distance = pointInfo.get(1) * 10; // 计算距离
                int time = calculateTime(start, pointInfo, distance); // 计算任务完成时间
                pointInfo.add(distance);
                pointInfo.add(time); // 添加距离和完成时间
                points.add(pointInfo);
            }

            // 对任务进行排序
            sortPoints(points);

            // 筛选有效任务
            List<List<Integer>> validPoints = filterValidPoints(start, end, points);

            // 输出结果
            printResults(validPoints);
        }
    }

    // 将时间转换为分钟数
    private static List<Integer> parseInput(String input) {
        List<Integer> parsedData = new ArrayList<>();
        String[] parts = input.split(" ");
        for (String part : parts) {
            parsedData.add(Integer.parseInt(part));
        }
        return parsedData;
    }

    // 计算从00:00开始的时间分钟数
    private static int calculateTimeInMinutes(List<Integer> timeInfo) {
        return timeInfo.get(0) * 60 + timeInfo.get(1);
    }

    // 计算任务的完成时间
    private static int calculateTime(int start, List<Integer> pointInfo, int distance) {
        int time = distance;
        int remainingQueue = Math.max(0, pointInfo.get(2) - distance);

        int arriveTime = start + distance;

        // 根据不同的时间段进行处理
        if (arriveTime <= MAX_WAIT_TIME) {
            return MAX_WAIT_TIME - start;
        } else if (arriveTime <= FIRST_STAGE_END_TIME) {
            remainingQueue += (arriveTime - MAX_WAIT_TIME) * 3 - (arriveTime - MAX_WAIT_TIME);
        } else if (arriveTime <= SECOND_STAGE_END_TIME) {
            // 第二阶段无需特别处理
        } else if (arriveTime <= THIRD_STAGE_END_TIME) {
            remainingQueue += (arriveTime - SECOND_STAGE_END_TIME) * 10 - (arriveTime - SECOND_STAGE_END_TIME);
        }

        return time + remainingQueue;
    }

    // 对任务列表进行排序
    private static void sortPoints(List<List<Integer>> points) {
        Collections.sort(points, (a, b) -> {
            if (a.get(4).equals(b.get(4))) {
                return b.get(3) - a.get(3);
            } else {
                return a.get(4) - b.get(4);
            }
        });
    }

    // 筛选在规定时间内完成的有效任务
    private static List<List<Integer>> filterValidPoints(int start, int end, List<List<Integer>> points) {
        List<List<Integer>> validPoints = new ArrayList<>();
        for (List<Integer> point : points) {
            int arrivalTime = start + point.get(4);
            if (arrivalTime < end) {
                validPoints.add(point);
            }
        }
        return validPoints;
    }

    // 输出最终结果
    private static void printResults(List<List<Integer>> points) {
        System.out.println(points.size());
        for (List<Integer> point : points) {
            System.out.println(point.get(0) + " " + point.get(4) + " " + point.get(3));
        }
    }
}